/**
 * 
 */
package com.tvt.sexyapp_demo.page;

import java.util.ArrayList;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.tvt.sexyapp_demo.appconfig.manager.PageInfoManager;
import com.tvt.sexyapp_demo.appconfig.xml.XItem;
import com.tvt.sexyapp_demo.debug.Debug;
import com.tvt.sexyapp_demo.fragment.MyListFragment;

/**
 * @author MR_HIEU
 * 
 */
public class LocalPageController {

	// Constant
	private final String LOG_TAG = this.getClass().getSimpleName();
	// Fields

	private final int MAX_PAGE_NUMBER = PageInfoManager.getInstance().getMaxPageNumber();
	private final int MIN_PAGE_NUMBER = PageInfoManager.getInstance().getMinPageNumber();

	private MyListFragment mMyListFragment;
	private Button mBtnPrevious;
	private Button mBtnNext;
	private Button[] mBtnPage;

	public LocalPageController(MyListFragment pMyListFragment, Button pBtnPrevious, Button pBtnNext,
			Button... pBtnPage) {

		this.mMyListFragment = pMyListFragment;
		this.mBtnPrevious = pBtnPrevious;
		this.mBtnNext = pBtnNext;
		this.mBtnPage = pBtnPage;

		// this.registerEventHandler();
	}

	private void reCalulateItemsOnPage() {
		this.mMyListFragment.refreshPage();
	}

	private void registerEventHandler() {

		this.registerEventForBtnNext();
		this.registerEventForBtnPrevious();
		this.registerEventForBtnPages();
	}

	private void registerEventForBtnNext() {

		this.mBtnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				int currPageNumber = PageInfoManager.getInstance().getCurrentPageNumber();

				if (currPageNumber < MAX_PAGE_NUMBER) {

					int new_index = PageInfoManager.getInstance().getCurrentIndex() + 1;
					int new_page_number = currPageNumber + 1;

					PageInfoManager.getInstance().setCurrentIndex(new_index);
					PageInfoManager.getInstance().setCurrentPageNumber(new_page_number);

					refreshPage(new_page_number, new_index);

				} else
					Debug.w(LOG_TAG, "You were at last page, so can't go its previous page!");
			}
		});
	}

	private void registerEventForBtnPrevious() {

		this.mBtnPrevious.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (PageInfoManager.getInstance().getCurrentPageNumber() > PageInfoManager.getInstance()
						.getMinPageNumber()) {

					int new_index = PageInfoManager.getInstance().getCurrentIndex() - 1;

					int new_page_number = PageInfoManager.getInstance().getCurrentPageNumber() - 1;

					PageInfoManager.getInstance().setCurrentIndex(new_index);
					PageInfoManager.getInstance().setCurrentPageNumber(new_page_number);

					refreshPage(new_page_number, new_index);

				} else
					Debug.w(LOG_TAG, "You were at first page, so can't go its previous page!");

			}
		});
	}

	private void registerEventForBtnPages() {

		for (int i = 0; i < this.mBtnPage.length; i++) {

			final int new_index = i;
			this.mBtnPage[i].setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {

					int old_page_number = PageInfoManager.getInstance().getCurrentPageNumber();
					int old_index = PageInfoManager.getInstance().getCurrentIndex();

					int new_page_number = old_page_number - old_index + 1;

					PageInfoManager.getInstance().setCurrentPageNumber(new_page_number);
					PageInfoManager.getInstance().setCurrentIndex(new_index);

					refreshPage(new_page_number, new_index);
				}
			});
		}
	}

	private void refreshPage(int pCurrPageNumber, int pCurrIndex) {

		for (int i = 0; i < this.mBtnPage.length; i++) {

			this.mBtnPage[i].setText(String.valueOf(pCurrPageNumber - pCurrIndex + i));
		}

		// if (pCurrPageNumber == MAX_PAGE_NUMBER) {
		//
		// this.mBtnNext.setEnabled(false);
		//
		// } else if (pCurrPageNumber == MIN_PAGE_NUMBER) {
		//
		// } else {
		//
		// if (!this.mBtnNext.isEnabled())
		// this.mBtnNext.setEnabled(true);
		//
		// if (!this.mBtnPrevious.isEnabled())
		// this.mBtnPrevious.setEnabled(true);
		// }

		this.reCalulateItemsOnPage();
	}
}
